package com.ihg.hiex.admin.web.controller.certificate;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.ihg.hiex.admin.entity.search.CertificateAssessorSearch;
import com.ihg.hiex.admin.service.account.AssessorCertificateRecordService;
import com.ihg.hiex.admin.service.account.AssessorService;
import com.ihg.hiex.entity.user.Assessor;
import com.ihg.hiex.entity.user.AssessorCertificateRecord;

/**
 * 
 * @author dan
 *
 */
@Controller
@RequestMapping("/certificate-assessors")
@MenuSetting("menu-settings")
public class CertificateAssessorController {

    private static final Logger logger = LoggerFactory.getLogger(CertificateAssessorController.class);
    
    @Autowired
    private AssessorService assessorService;
    
    @Autowired
    private AssessorCertificateRecordService assessorCertificateRecordService;
    
    /**
     * list assessor by page
     * 
     * @param page
     * @return
     */
    @MenuSetting("submenu-assessor-certificate-management")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(Page page) {
        
        ModelAndView modelAndView = new ModelAndView("certificate/certificate-assessor/certificate-assessor-list");
        
        int totals = assessorCertificateRecordService.count(AssessorCertificateRecord.class);
        if (totals > 0) {
            
            List<AssessorCertificateRecord> records = assessorCertificateRecordService.listByPage(AssessorCertificateRecord.class, page.with(totals));
            
            modelAndView.addObject("records",records);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * display search panel
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String search() {
        return "certificate/certificate-assessor/certificate-assessor-search";
    }

    /**
     * on search assessor
     * 
     * @param search
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-assessor-certificate-management")
    public ModelAndView onSearch(CertificateAssessorSearch search, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("certificate/certificate-assessor/certificate-assessor-list");
        
        int totals = assessorCertificateRecordService.count(AssessorCertificateRecord.class, search.getPreparedFilter(false));
        if (totals > 0) {
            
            List<AssessorCertificateRecord> records = assessorCertificateRecordService.search(AssessorCertificateRecord.class, search, page.with(totals));
            modelAndView.addObject("records",records);
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString",WebUtils.extractQueryString(request, "page","filter"));

        }
        
        return modelAndView;
        
    }
    
    /**
     * view assessor
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}" , method = RequestMethod.GET)
    @MenuSetting("submenu-assessor-certificate-management")
    public ModelAndView view(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("certificate/certificate-assessor/certificate-assessor-view");

        // 1. load assessor info
        AssessorCertificateRecord record = assessorCertificateRecordService.load(AssessorCertificateRecord.class, id);
        Assessor assessor = record.getAssessor();
        modelAndView.addObject("assessor", assessor);
        
        // 2. list this assessor's all record
        List<AssessorCertificateRecord> records = assessorCertificateRecordService.list(assessor);
        modelAndView.addObject("records", records);
        
        return modelAndView;
        
    }
    
    /**
     * prepare to create assessor
     * 
     * @return
     */
    @RequestMapping(value = "/create" , method = RequestMethod.GET)
    public String create() {
        return "certificate/certificate-assessor/certificate-assessor-create";
    }
    
    /**
     * create a new assessor
     * 
     * @param assessor
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(Assessor assessor) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            assessorService.createCertificateAssessor(assessor, false);
            
            httpResult.success().message("增加成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("增加失败");
            logger.error("#onCreate() - create fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
    
    /**
     * prepare to create assessor
     * 
     * @return
     */
    @RequestMapping(value = "/bind" , method = RequestMethod.GET)
    public ModelAndView bind() {
        ModelAndView modelAndView = new ModelAndView("certificate/certificate-assessor/certificate-assessor-bind");
        
        List<AssessorCertificateRecord> records = assessorCertificateRecordService.listAll(AssessorCertificateRecord.class);
        modelAndView.addObject("assessors", CollectionUtils.extractPropertyList(records, "assessor"));
        
        return modelAndView;
    }
    
    /**
     * create a new assessor
     * 
     * @param assessor
     * @return
     */
    @RequestMapping(value = "/bind" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onBind(@RequestParam("assessorId") Long assessorId) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            Assessor assessor = assessorService.load(Assessor.class, assessorId);
            assessorService.createCertificateAssessor(assessor, true);
            
            httpResult.success().message("增加成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("增加失败");
            logger.error("#onCreate() - create fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
    
}